package com.xueyi.system.plantManager.controller;

import com.xueyi.system.parcel.domain.dto.BaseParcelDto;
import com.xueyi.system.parcel.service.IBaseParcelService;
import com.xueyi.system.plantManager.domain.query.BasePlantManagerQuery;
import com.xueyi.system.plantManager.domain.dto.BasePlantManagerDto;
import com.xueyi.system.plantManager.service.IBasePlantManagerService;
import com.xueyi.common.web.entity.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 种植管理管理 业务处理
 *
 * @author zlf
 */
@RestController
@RequestMapping("/plantManager")
public class BasePlantManagerController extends BaseController<BasePlantManagerQuery, BasePlantManagerDto, IBasePlantManagerService> {


    @Autowired
    private IBasePlantManagerService basePlantManagerService;

    @Autowired
    private IBaseParcelService baseParcelService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "种植管理" ;
    }

    /**
     * 查询种植管理列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.BASE_PLANT_MANAGER_LIST)
    public AjaxResult list(BasePlantManagerQuery plantManager) {
        return super.list(plantManager);
    }

    /**
     * 查询种植管理详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.BASE_PLANT_MANAGER_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 种植管理导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.BASE_PLANT_MANAGER_EXPORT)
    @Log(title = "种植管理管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, BasePlantManagerQuery plantManager) {
        super.export(response, plantManager);
    }

    /**
     * 种植管理新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.BASE_PLANT_MANAGER_ADD)
    @Log(title = "种植管理管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody BasePlantManagerDto plantManager) {
        return super.add(plantManager);
    }

    /**
     * 种植管理修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.BASE_PLANT_MANAGER_EDIT)
    @Log(title = "种植管理管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody BasePlantManagerDto plantManager) {
        return super.edit(plantManager);
    }

    /**
     * 种植管理批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.BASE_PLANT_MANAGER_DEL)
    @Log(title = "种植管理管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取种植管理选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }


    /**
     * 当前种植品种
     * @return
     */
    @GetMapping("/plantBreed")
    public AjaxResult plantBreed(){
        BasePlantManagerQuery managerQuery = new BasePlantManagerQuery();
        managerQuery.setPlantStatus("1");
        List<BasePlantManagerDto> dtoList = basePlantManagerService.selectList(managerQuery);
        List list = new ArrayList<>();
        for (BasePlantManagerDto basePlantManagerDto : dtoList) {
            list.add(basePlantManagerDto);
        }
        long num = list.stream().distinct().count();

        return AjaxResult.success(num);
    }

    /**
     * 当前种植面积
     */

    @GetMapping("/plantNowArea")
    public AjaxResult plantNowArea(){
        BasePlantManagerQuery managerQuery = new BasePlantManagerQuery();
        managerQuery.setPlantStatus("1");
        List<BasePlantManagerDto> dtoList = basePlantManagerService.selectList(managerQuery);
        BigDecimal decimal = new BigDecimal("0");
        for (BasePlantManagerDto managerDto : dtoList) {
            decimal = decimal.add(managerDto.getEstimatedPerMu());
        }

        return AjaxResult.success(decimal);
    }


    interface Auth {
        /** 系统 - 种植管理管理 - 列表 */
        String BASE_PLANT_MANAGER_LIST = "system:plantManager:list" ;
        /** 系统 - 种植管理管理 - 详情 */
        String BASE_PLANT_MANAGER_SINGLE = "system:plantManager:single" ;
        /** 系统 - 种植管理管理 - 新增 */
        String BASE_PLANT_MANAGER_ADD = "system:plantManager:add" ;
        /** 系统 - 种植管理管理 - 修改 */
        String BASE_PLANT_MANAGER_EDIT = "system:plantManager:edit" ;
        /** 系统 - 种植管理管理 - 删除 */
        String BASE_PLANT_MANAGER_DEL = "system:plantManager:delete" ;
        /** 系统 - 种植管理管理 - 导出 */
        String BASE_PLANT_MANAGER_EXPORT = "system:plantManager:export" ;
    }
}
